package domain;

import java.io.File;
import java.net.InetAddress;
import java.util.LinkedList;

import javax.sound.sampled.AudioInputStream;
import javax.swing.table.TableModel;

import com.audiobuddy.serializables.Song;
import com.audiobuddy.serializables.SongCommand;

import player.MusicPlayer;

import tcp.TCPController;
import udp.UDPController;
import gui.GuiController;

public class ServerDomainController {

	private GuiController gc;
	private UDPController udpController;
	private SongTableModel songTableModel;
	private MusicPlayer player;
	private TCPController tcpController;

	public void run() {
		gc = new GuiController(this);
				//---------------------testGuistuff-------------------------
				//songTableModel = new SongTableModel();
				//songTableModel.addSong(new Song("Dream On","Aerosmith",184,null, 0));
				//---------------------testGuistuff----------------------------
		udpController = new UDPController(this);
		tcpController = new TCPController(this);
		player = new MusicPlayer(this);
	}
	
	
	
	public TableModel getSongTableModel() {
		if(songTableModel==null){
			songTableModel = new SongTableModel();
		}
		return songTableModel;
	}
	
	public LinkedList<Song> getSongs(){
		return songTableModel.getSongs();
	}



	public void multicastPlaylist() {
		udpController.multicastPlaylist();
		
	}



	public void addSong(Song song) {
		songTableModel.addSong(song);
		udpController.multicastPlaylist();
		
	}



	public void executeSongCommand(SongCommand command) {
		int commandType = command.getCommand();
		Song song;
		switch(commandType){
		case SongCommand.PLAY:
			song = songTableModel.getSong(command.getSongRowId());
			System.out.println("SERVER=> start playing: " + song.getArtist());
			try{
				player.stop();
			}catch (Exception e) {
				System.out.println("exc on stop before play "+e);
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			player.playNew(song);
			break;
		case SongCommand.PAUSE:
			
			break;
		case SongCommand.STOP:
			System.out.println("received stop command");
			player.stop();
			break;
		case SongCommand.NEXT:
			song = player.getCurrentSong();
			
			System.out.println("SERVER=> start playing: " + song.getArtist());
			try{
				player.stop();
			}catch (Exception e) {
				System.out.println("exc on stop before play "+e);
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			player.playNew(songTableModel.getNext(song));
			
			break;
		case SongCommand.PREVIOUS:
			song = player.getCurrentSong();
			
			System.out.println("SERVER=> start playing: " + song.getArtist());
			try{
				player.stop();
			}catch (Exception e) {
				System.out.println("exc on stop before play "+e);
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			player.playNew(songTableModel.getPrevious(song));
			break;
			
		case SongCommand.STOP_BROADCASTING:
			InetAddress broadcasterIp = player.getBroadcasterIp();
			if(broadcasterIp!=null){
				udpController.stopBroadcasting(broadcasterIp);
			}
			break;
		case SongCommand.DELETE:
			System.out.println(command.getSongRowId());
			songTableModel.deleteSong(command.getSongRowId());
			udpController.multicastPlaylist();
			
		}
		
	}



	public void requestStartStreamingSong(Song song) {
		udpController.requestStartStreamingSong(song);
		
	}



	public void stopSong() {
		player.stop();
		
	}



	public File tcpAcceptFile() {
		return tcpController.acceptFile();
	}



	public void setCurrentSong(Song currentSong) {
		gc.setCurrentSong(currentSong);
		udpController.multicastCurrentlyPlaying(currentSong);
		
	}



	public void setVolume(int value) {
		player.setVolume(value);
		
	}

}
